/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.task;

import java.util.List;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.activity.intent.TaskIntentHolder;
import lt.marsrutai.mobile.android.exception.DataAccessException;
import lt.marsrutai.mobile.android.model.Track;
import lt.marsrutai.mobile.android.service.DataService;
import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;

/**
 * Fetches tracks. Calls:
 * <ul>
 *  <li>{@link DataService#getTracksWithIds(String, String[])} if <code>trackIds</code> are provided</li>
 *  <li>{@link DataService#getTracks(String, String)} otherwise.</li>
 * </ul>
 * 
 * @author Justas
 */
public class GetTracksTask extends NotifyingAsyncTask<GetTracksTask.Criteria, Void, List<Track>> {
    
    public static final String BROADCAST_ACTION = "lt.marsrutai.mobile.android.task.GetTracksTask";

    protected List<Track> doInBackground(Criteria... params) {
        if (params.length != 1) {
            throw new IllegalArgumentException("Only one criteria must be provided for GetTracksTask.");
        }
        
        try {
            String[] trackIds = params[0].getTrackIds();
            String scopeFolder = params[0].getScopeFolder();
            
            if (trackIds == null) {
                String scheduleId = params[0].getScheduleId();
                return AppContext.getDataService().getTracks(scopeFolder, scheduleId);
            }
            else {
                return AppContext.getDataService().getTracksWithIds(scopeFolder, trackIds);
            }
        }
        catch (DataAccessException e) {
            setResultStatus(RESULT_NOT_RESOLVED);
        }

        return null;
    }
    
    @Override
    protected Intent getResultIntent(List<Track> result) {
        TaskIntentHolder intentHolder = new TaskIntentHolder(super.getResultIntent(result));
        intentHolder.setResult(result, Track.class);
        return intentHolder.getIntent();
    }

    @Override
    protected String getBroadcastAction() {
        return BROADCAST_ACTION;
    }
    
    public static class Criteria implements Parcelable {

        public static final Parcelable.Creator<Criteria> CREATOR = new Parcelable.Creator<Criteria>() {

            @Override
            public Criteria createFromParcel(Parcel source) {
                return new Criteria(source);
            }

            @Override
            public Criteria[] newArray(int size) {
                return new Criteria[size];
            }
        };
        
        private String scopeFolder;
        private String scheduleId;
        private String[] trackIds;

        public Criteria() {
        }

        public Criteria(String scopeName, String scheduleId, String[] trackIds) {
            this.scopeFolder = scopeName;
            this.scheduleId = scheduleId;
            this.trackIds = trackIds;
        }
        
        public Criteria(Parcel in) {
            scopeFolder = in.readString();
            scheduleId = in.readString();
            in.readStringArray(trackIds);
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(scopeFolder);
            dest.writeString(scheduleId);
            dest.writeStringArray(trackIds);
        }

        @Override
        public int describeContents() {
            return 0;
        }
        
        public void setTrackIds(String[] trackIds) {
            this.trackIds = trackIds;
        }

        public String getScopeFolder() {
            return scopeFolder;
        }
        
        public void setScopeFolder(String scopeFolder) {
            this.scopeFolder = scopeFolder;
        }
        
        public String getScheduleId() {
            return scheduleId;
        }
        
        public void setScheduleId(String scheduleId) {
            this.scheduleId = scheduleId;
        }
        
        public String[] getTrackIds() {
            return trackIds;
        }
        
    }

}
